package com.example.mypra1;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.szysky.customize.siv.SImageView;

/**
 * 编辑个人资料
 */
public class UserInfoActivity extends AppCompatActivity {
    private String profile,username,sex,shengxiao,xinzuo;
    private SImageView profile_iv;//头像
    private TextView username_tv,sex_tv,shengxiao_tv,xinzuo_tv;
    private RelativeLayout profile_btn,username_btn,sex_btn,shengxiao_btn,xinzuo_btn,pwd_btn,save_btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        profile_iv = findViewById(R.id.profile);
        username_tv = findViewById(R.id.username);
        sex_tv = findViewById(R.id.sex);
        shengxiao_tv = findViewById(R.id.shengxiao);
        xinzuo_tv = findViewById(R.id.xinzuo);
        //获取存储在SharedPreference中的当前用户的信息
        SharedPreferences pref = getSharedPreferences("data", Context.MODE_PRIVATE);
        profile = pref.getString("profile","");
        username = pref.getString("username","");
        int sex_num = pref.getInt("sex",0);
        sex = sex_num==0?"男":"女";
        shengxiao = pref.getString("zodiac","龙");
        xinzuo = pref.getString("constellation","双子座");
        profile_iv.setImageUrls(profile);
        username_tv.setText(username);
        sex_tv.setText(sex);
        shengxiao_tv.setText(shengxiao);
        xinzuo_tv.setText(xinzuo);
        username_btn = findViewById(R.id.username_btn);
        sex_btn = findViewById(R.id.sex_btn);
        shengxiao_btn = findViewById(R.id.shengxiao_btn);
        xinzuo_btn = findViewById(R.id.xinzuo_btn);
        username_btn.setOnClickListener(new View.OnClickListener() {//修改昵称
            @Override
            public void onClick(View view) {
                final EditText text = new EditText(UserInfoActivity.this);
                new AlertDialog.Builder(UserInfoActivity.this)
                        .setTitle("请输入修改后的昵称：")
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setView(text)

                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 获取输入框的内容
                                username = text.getText().toString();
                                username_tv.setText(username);
                                SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
                                editor.putString("username",username);
                                editor.commit();
                                Toast.makeText(UserInfoActivity.this, "修改成功!", Toast.LENGTH_SHORT).show();
                                //dialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                                dialog.dismiss();
                            }
                        }).show();
            }
        });
        sex_btn.setOnClickListener(new View.OnClickListener() {//修改性别
            @Override
            public void onClick(View view) {
                new AlertDialog.Builder(UserInfoActivity.this)
                        .setTitle("选择修改后的性别")
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setSingleChoiceItems(new String[]{"男","女"}, 0, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 选中后实现的操作
                                if(which == 0)
                                    sex_tv.setText("男");
                                else
                                    sex_tv.setText("女");
                                Toast.makeText(UserInfoActivity.this, "修改成功", Toast.LENGTH_SHORT).show();

                            }
                        }).show();
            }
        });
        shengxiao_btn.setOnClickListener(new View.OnClickListener() {//修改生肖
            @Override
            public void onClick(View view) {
                String s[]={"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};
                new AlertDialog.Builder(UserInfoActivity.this)
                        .setTitle("选择修改后的生肖")
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setSingleChoiceItems(s, 0, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 选中后实现的操作
                                shengxiao = s[which];
                                shengxiao_tv.setText(shengxiao);
                                Toast.makeText(UserInfoActivity.this, "修改成功", Toast.LENGTH_SHORT).show();

                            }
                        }).show();
            }
        });
        xinzuo_btn.setOnClickListener(new View.OnClickListener() {//修改星座
            @Override
            public void onClick(View view) {
                String s[] = {"白羊座","金牛座","双子座","巨蟹座","狮子座",
                        "处女座","天枰座","天蝎座","射手座","魔蝎座","水瓶座","双鱼座"};
                new AlertDialog.Builder(UserInfoActivity.this)
                        .setTitle("选择修改后的星座")
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setSingleChoiceItems(s, 0, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 选中后实现的操作
                                xinzuo = s[which];
                                xinzuo_tv.setText(xinzuo);
                                Toast.makeText(UserInfoActivity.this, "修改成功", Toast.LENGTH_SHORT).show();

                            }
                        }).show();
            }
        });
        save_btn = findViewById(R.id.save_btn);
        save_btn.setOnClickListener(new View.OnClickListener() {//保存更改后端数据
            @Override
            public void onClick(View view) {
                Toast.makeText(UserInfoActivity.this,"保存成功！",Toast.LENGTH_LONG).show();
            }
        });
        pwd_btn = findViewById(R.id.psw_btn);
        pwd_btn.setOnClickListener(new View.OnClickListener() {//修改密码
            @Override
            public void onClick(View view) {
                LinearLayout linearLayout = new LinearLayout(UserInfoActivity.this);
                //实现两个编辑框，原密码和新密码
                final TextView[] textViews = new TextView[2];
                final EditText[] editTexts = new EditText[2];
                linearLayout.setOrientation(LinearLayout.VERTICAL);
                for(int i=0;i<textViews.length;i++){
                    textViews[i] = new TextView(UserInfoActivity.this);
                    textViews[i].setTextSize(20);
                    editTexts[i] = new EditText(UserInfoActivity.this);
                    if(i==0){
                        textViews[i].setText("旧密码：");
                    }else {
                        textViews[i].setText("新密码：");
                    }
                    linearLayout.addView(textViews[i]);
                    linearLayout.addView(editTexts[i]);
                }
                editTexts[1].setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
                linearLayout.setPadding(15,15,15,15);
                new AlertDialog.Builder(UserInfoActivity.this)
                        .setTitle("修改密码")
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setView(linearLayout)

                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 获取输入框的内容
                                Toast.makeText(UserInfoActivity.this, "修改成功!", Toast.LENGTH_SHORT).show();
                                //dialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                                dialog.dismiss();
                            }
                        }).show();

            }
        });
    }
}